﻿using System;

namespace SobrietyEngine.Common
{
    public class StringDataParser
    {
        //Generic parser for this style noNewline:
        //used throughout the gameServices
        /*
         * Token //comment
         * {
         *      key = value;
         *      token
         *      {
         *          key = value;
         *      }
         * }
         * 
         */
        public static int nextIndexOf(int pos, ref string data, char[] chars)
        {
            for(;pos < data.Length;++pos)
                foreach (char c in chars)
                    if (data[pos] == c)
                        return pos;
            return -1;
        }

        public static void moveToNextLine(ref int pos, ref String data)
        {
            while (pos < data.Length && data[pos] != '\n')
                ++pos;
            ++pos;
        }

        public static String getLine(ref int pos, ref String data)
        {
            String line = "";
            int tempPos = pos;
            while (tempPos < data.Length && data[tempPos] != '\n')
            {
                line += data[tempPos];
                ++tempPos;
            }
            return line;
        }

        public static String getPrevLine(ref int pos, ref String data, int prevStartPos)
        {
            String line = "";
            int firstPrevNL = pos;
            //this gets the index of the first newline
            while (firstPrevNL >= 0 && data[firstPrevNL] != '\n')
                firstPrevNL--;

            //find one mor eback. our last line is between the last two new lines
            int secondPrevNL = firstPrevNL - 1;
            while (secondPrevNL >= 0 && data[secondPrevNL] != '\n')
                secondPrevNL--;

            //our previous line is between second and first newlines
            line = data.Substring(secondPrevNL + 1, firstPrevNL - (secondPrevNL - 1));
            prevStartPos = secondPrevNL + 1;

            return line;
        }

        public static String stripComments(ref String data, string commentString)
        {
            int pos = 0;
            String noComments = data;
            while ((pos = noComments.IndexOf(commentString)) >= 0)
            {
                int endl = noComments.IndexOf('\n', pos);
                String first = noComments.Substring(0, pos);
                String second = noComments.Substring(endl + 1, (noComments.Length - 1) - endl);
                noComments = first + second;
            }
            return noComments;
        }

        public static String stripNewlines(ref String data)
        {
            int pos = 0;
            String noNewline = data;
            while ((pos = noNewline.IndexOf('\n')) >= 0)
            {
                String first = noNewline.Substring(0, pos);
                String second = noNewline.Substring(pos + 1, (noNewline.Length - 1) - (pos));
                noNewline = first + second;
            }
            return noNewline;
        }

        public static String stripSpaces(ref String data)
        {
            int pos = 0;
            String noSpaces = data;
            while ((pos = noSpaces.IndexOf(' ')) >= 0)
            {
                String first = noSpaces.Substring(0, pos);
                String second = noSpaces.Substring(pos + 1, (noSpaces.Length - 1) - (pos));
                noSpaces = first + second;
            }
            return noSpaces;
        }

        public static void movePastWhiteSpace(ref int pos, ref String data)
        {
            while (data[pos] == ' ' || data[pos] == '\n')
                ++pos;
        }

        public static String prevStringBetween(int pos, ref string data, char[] betweenMarkers)
        {

            //scan backwards looking for the first args of the marker
            String line = "";
            int firstPrevNL = pos;
            //this gets the index of the first newline
            while (firstPrevNL >= 0 && isNotIn(data[firstPrevNL], betweenMarkers))
                firstPrevNL--;

            //find one mor eback. our last line is between the last two new lines
            int secondPrevNL = firstPrevNL - 1;
            while (secondPrevNL >= 0 && isNotIn(data[secondPrevNL], betweenMarkers))
                secondPrevNL--;

            //our previous line is between second and first newlines
            line = data.Substring(secondPrevNL + 1, firstPrevNL - (secondPrevNL - 1));
            //prevStartPos = secondPrevNL + 1;

            return line;
        }

        public static String stringBetween(int pos, ref string data, char[] betweenMarkers)
        {

            //scan backwards looking for the first args of the marker
            String line = "";
            int first = pos;

            //find one mor eback. our last line is between the last two new lines
            int second = first + 1;
            while (second >= 0 && isNotIn(data[second], betweenMarkers))
                second++;

            //our previous line is between second and first newlines
            line = data.Substring(first, second - first);
            foreach (Char c in betweenMarkers)
            {
                line = line.Replace(c.ToString(), "");
            }

            return line;
        }

        public static Boolean isNotIn(char c, char[] checkChars)
        {
            foreach (Char s in checkChars)
                if (s == c)
                    return false;
            return true;
        }
    }
}
